package com.redwine.app.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.redwine.app.PublishedSourcesDetailActivity;
import com.redwine.app.R;
import com.redwine.app.net.responses.SupplyOfGoodsResponse;
import com.redwine.app.utils.BitmapUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 货源的适配器
 * 作者：gassion on 17/7/27 14:38
 * 最可怕的敌人，就是没有坚强的信念。
 */
public class PublishedSourcesAdapter extends BaseAdapter {

    private Context context;

    private List<SupplyOfGoodsResponse.DataEntityX.DataEntity> datalist;


    public PublishedSourcesAdapter(Context context) {
        this.context = context;
    }


    /**
     * 下拉加载数据
     *
     * @param datalist
     */
    public void setData(int page, List<SupplyOfGoodsResponse.DataEntityX.DataEntity> datalist) {
        if (page > 1) {
            if (null != this.datalist && !this.datalist.isEmpty())
                this.datalist.addAll(datalist);

        } else {
            if (null != this.datalist && !this.datalist.isEmpty())
                this.datalist.clear();
            this.datalist = datalist;
        }
        notifyDataSetChanged();
    }

    public List<SupplyOfGoodsResponse.DataEntityX.DataEntity> getData() {
        if (null != datalist && !datalist.isEmpty()) {
            return datalist;
        } else {
            return new ArrayList<>();
        }
    }

    @Override
    public int getCount() {
        if (null != datalist) {
            return datalist.size();
        } else {
            return 0;
        }
    }

    @Override
    public Object getItem(int position) {
        if (null != datalist) {
            return datalist.get(position);
        } else {
            return null;
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            LayoutInflater layoutInflater = LayoutInflater.from(context);
            convertView = layoutInflater.inflate(R.layout.fragment_supply_of_goods_item, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        final SupplyOfGoodsResponse.DataEntityX.DataEntity dataEntity = datalist.get(position);
        List<String> strings = strConvertList(dataEntity.getPic());
        if (null != strings && !strings.isEmpty())
            BitmapUtils.loadImage(viewHolder.itemIvShopPic, strings.get(0), BitmapUtils.THE_ARTICLE);
        viewHolder.itemTvShopName.setText(dataEntity.getName());
        viewHolder.itemTvContent.setText(dataEntity.getCategory());
        viewHolder.itemTvPrice.setText(dataEntity.getPrice());
        viewHolder.itemTvCity.setText(dataEntity.getCity());
        viewHolder.itemTvProduce.setText(dataEntity.getProduce());
        viewHolder.itemTvProcurement.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, PublishedSourcesDetailActivity.class);
                intent.putExtra(PublishedSourcesDetailActivity.SHOP_NAME, dataEntity.getName());
                intent.putExtra(PublishedSourcesDetailActivity.SHOP_UID, dataEntity.getUid());
                intent.putExtra(PublishedSourcesDetailActivity.SHOP_ID, dataEntity.getId());
                context.startActivity(intent);
            }
        });
        return convertView;
    }

    public static class ViewHolder {
        public View rootView;
        public ImageView itemIvShopPic;
        public TextView itemTvShopName;
        public TextView itemTvContent;
        public TextView itemTvPrice;
        public TextView itemTvCity;
        public TextView itemTvProduce;
        public TextView itemTvProcurement;

        public ViewHolder(View rootView) {
            this.rootView = rootView;
            this.itemIvShopPic = (ImageView) rootView.findViewById(R.id.item_iv_shop_pic);
            this.itemTvShopName = (TextView) rootView.findViewById(R.id.item_tv_shop_name);
            this.itemTvContent = (TextView) rootView.findViewById(R.id.item_tv_content);
            this.itemTvPrice = (TextView) rootView.findViewById(R.id.item_tv_price);
            this.itemTvCity = (TextView) rootView.findViewById(R.id.item_tv_city);
            this.itemTvProduce = (TextView) rootView.findViewById(R.id.item_tv_produce);
            this.itemTvProcurement = (TextView) rootView.findViewById(R.id.item_tv_procurement);
        }

    }

    /**
     * 传递一个，分割的字符串返回一个集合
     *
     * @param content
     * @return
     */
    public List<String> strConvertList(String content) {
        List<String> list = new ArrayList<>();
        if (!TextUtils.isEmpty(content)) {
            String[] split = content.split(",");
            for (int i = 0; i < split.length; i++) {
                list.add(split[i]);
            }
            return list;
        } else {
            return new ArrayList<>();
        }
    }
}
